const http = require('../../../../utils/http.js')
const Base64 = require('../../../../utils/base64.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        inputArr: [],
        is_default: false,
        is_Choose: false,
        clickF: false,
        choose_flag: [true, false, false],
        bind_flag: [true, false, false]
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },


    /**
     *  获取所有INPUT的数据
     */
    inputTxt(e) {
        let inputArr = this.data.inputArr
        let temp = http.inputArr(inputArr, e)
        // console.log(temp)
        this.setData({
            inputArr: temp
        })
    },

    /**
     * 设为默认地址
     */
    setDefault() {
        let is_default = this.data.is_default
        let that = this
        if (is_default) {
            that.setData({
                is_default: false
            })
        } else {
            that.setData({
                is_default: true
            })
        }
    },

    /**
     * 收货地址选择
     */
    chooseAddress() {
        let that = this,
            token = wx.getStorageSync('token')
        wx.showLoading({
            title: '加载中...',
            mask: true,
        })
        that.setData({
            is_Choose: true
        })
        wx.request({
            url: 'https://apis.map.qq.com/ws/district/v1/list?key=F3IBZ-44IKF-5CCJL-N3BTL-JBUZE-UIFBA',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            method: 'GET',
            success(res) {
                // console.log(res.data)
                wx.hideLoading()
                let data = res.data
                that.setData({
                    addressArr: data.result[0]
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },

    /**
     * 选择市
     */
    proChoose(e) {
        let that = this,
            id = http.dataIndex(e)[1],
            name = http.dataIndex(e)[3],
            choose_flag = that.data.choose_flag,
            bind_flag = that.data.bind_flag
        choose_flag[1] = true
        bind_flag[1] = true
        wx.request({
            url: 'https://apis.map.qq.com/ws/district/v1/getchildren?&id=' + id + '&key=F3IBZ-44IKF-5CCJL-N3BTL-JBUZE-UIFBA',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            method: 'GET',
            success(res) {
                console.log(res.data)
                wx.hideLoading()
                let data = res.data
                that.setData({
                    addressArr: data.result[0],
                    pro: name,
                    choose_flag: that.data.choose_flag,
                    bind_flag: that.data.bind_flag
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },

    /**
     * 选择区县
     */
    cityChoose(e) {
        let that = this,
            id = http.dataIndex(e)[1],
            name = http.dataIndex(e)[3],
            choose_flag = that.data.choose_flag,
            bind_flag = that.data.bind_flag
        choose_flag[2] = true
        bind_flag[2] = true
        wx.request({
            url: 'https://apis.map.qq.com/ws/district/v1/getchildren?&id=' + id + '&key=F3IBZ-44IKF-5CCJL-N3BTL-JBUZE-UIFBA',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            method: 'GET',
            success(res) {
                // console.log(res.data)
                wx.hideLoading()
                let data = res.data
                that.setData({
                    addressArr: data.result[0],
                    city: name,
                    choose_flag: that.data.choose_flag,
                    bind_flag: that.data.bind_flag
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },

    /**
     * 点击确认
     */
    countyChoose(e) {
        let that = this,
            id = http.dataIndex(e)[1],
            name = http.dataIndex(e)[3]
        that.setData({
            county: name
        })
        setTimeout(function () {
            that.setData({
                is_Choose: false
            })
        }, 300)
        let inputArr = that.data.inputArr
        inputArr[2] = that.data.pro + that.data.city + that.data.county
        that.setData({
            inputArr: that.data.inputArr,
            bind_flag: [true, false, false]
        })
    },

    /**
     * 关闭地址选择
     */
    closeChoose() {
        this.setData({
            is_Choose: false
        })
    },

    /**
     * 确认添加或者修改
     */
    confirmSave() {
        let that = this,
            url = 'Ucenter_changeAddress',
            token = wx.getStorageSync('token'),
            aid = that.data.aid,
            type = aid ? 2 : 1,
            inputArr = that.data.inputArr,
            consignee = inputArr[0],
            tel = inputArr[1],
            add = inputArr[2],
            telephone = Base64.CusBASE64.encoder('UNO' + inputArr[1] + 'HACHA'),
            province = that.data.pro,
            city = that.data.city,
            county = that.data.county,
            address = inputArr[3],
            isDefault = that.data.is_default ? 2 : 1
            console.log(consignee)
            if (!consignee){
                    wx.showToast({
                      title: '请填写收货人',
                      icon:"none",
                      duration:3000
                    })
                    return false
                  }
            if (!tel) {
              wx.showToast({
                title: '请填写手机号',
                icon: "none",
                duration: 3000
              })
              return false
            }
            if (!add) {
              wx.showToast({
                title: '请选择所在地区',
                icon: "none",
                duration: 3000
              })
              return false
            }
            if (!address) {
              wx.showToast({
                title: '请填写详细地址',
                icon: "none",
                duration: 3000
              })
              return false
            }
        http._post(
            url,
            { token: token, aid: aid, type: type, consignee: consignee, telephone: telephone, province: province, city: city, county: county, address: address, isDefault: isDefault },
            res => {
                console.log(res.data)
                // wx.showToast({
                //     title: res.data.info,
                //     icon: 'none',
                //     duration: 3000
                // })
                if (res.data.re === '1') {
                    that.goBack()
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let id = options.id || '0',
            consignee = options.consignee || '',
            telephone = options.telephone || '',
            province = options.province || '',
            city = options.city || '',
            county = options.county || '',
            place = options.place || '',
            isDefault = options.isDefault || '',
            that = this,
            inputArr = that.data.inputArr

        inputArr[0] = consignee
        inputArr[1] = telephone
        inputArr[2] = province + city + county
        inputArr[3] = place

        if (id === '0') {
            that.setData({
                navBar: {
                    title: '新增收货地址',
                    btn: 'goBack'
                }
            })
        } else {
            that.setData({
                navBar: {
                    title: '编辑收货地址',
                    btn: 'goBack'
                },
                inputArr: inputArr,
                is_default: isDefault === '2' ? true : false,
                pro: province,
                city: city,
                county: county,
                aid: id,
                choose_flag: [true, true, true]
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})